/*
 * Created on Feb 11, 2008
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.phalanx.client.properties;

import java.util.Enumeration;
import java.util.Hashtable;

import org.phalanx.client.properties.xml.PhalanxConfigFileHandler;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * @author sday
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ClientPrefXMLReader {
	private String defaultXMLConfigFile = "./phalanxserver.xml";
	private ClientPreferences serverPrefs;

	public ClientPrefXMLReader(ClientPreferences serverPrefs, String serverConfigFile){
		defaultXMLConfigFile = serverConfigFile;
		this.serverPrefs = serverPrefs;
	}
	
	public ClientPrefXMLReader(ClientPreferences serverPrefs){
		this.serverPrefs = serverPrefs;
	}
	
	public void readXMLIntoPrefs(){
		// This method reads in the specified XML File and takes the 
		// stores the specified elements
		try {
			XMLReader configParser = XMLReaderFactory.createXMLReader();
			
			PhalanxConfigFileHandler configHandler = new PhalanxConfigFileHandler();
			
			configParser.setContentHandler(configHandler);
			configParser.parse(defaultXMLConfigFile);
			
			pushHashtableIntoPrefs(configHandler.getHashtable());
			
			return;
		} catch (Exception e){
			e.printStackTrace();
		}
	}
	
	private void pushHashtableIntoPrefs(Hashtable<String,String> keyValue){
		// Grab keys from Hashtable and push them into ServerPrefs
		serverPrefs.clearPrefs();
		
		Enumeration<String> e = keyValue.keys();
		
		while (e.hasMoreElements()){
			String stringKey = e.nextElement();
			serverPrefs.setProperties(stringKey.toLowerCase(), keyValue.get(stringKey));
		}
		return;
	}
	
	public static void main(String[] args){
		ClientPreferences pref = new ClientPreferences();
		ClientPrefXMLReader p = new ClientPrefXMLReader(pref);
		p.readXMLIntoPrefs();
		pref.getProperties("XMPPClient.Password");
	}
}
